package sk.fiit.lulco.gui;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import sk.fiit.lulco.grafy.Graph;
import sk.fiit.lulco.grafy.NewHEALAlgorithm;
import sk.fiit.lulco.grafy.Vertex;
import jargs.gnu.CmdLineParser;

public class Main {

	private static void printUsage() {
		System.err.println("Usage: java -jar coloring.jar [--gui] [--gen] [--ind] [--eli]" +
				" [--ngi] [--ttl] [--prevcol] [--cvmp] [--ncvmp]");
		System.err.println("Example java -jar coloring.jar --gui --gen 100 --ind 50 --eli 5" +
				" --ngi 45 --ttl 3 --prevcol 2 --cvmp 0,9 --ncvmp 0,1");
		System.err.println("If --gui is not set, --file is required.");
		System.err.println("See documentation for more details.");
	}
	
	private static void errorFound() {
		printUsage();
		System.exit(2);
	}
	
	public static void main(String[] args) {
		CmdLineParser parser = new CmdLineParser();
		CmdLineParser.Option gui = parser.addBooleanOption("gui");
		CmdLineParser.Option file = parser.addStringOption("file");
		CmdLineParser.Option generations = parser.addIntegerOption("gen");
		CmdLineParser.Option individuals = parser.addIntegerOption("ind");
		CmdLineParser.Option elite = parser.addIntegerOption("eli");
		CmdLineParser.Option ngi = parser.addIntegerOption("ngi");
		CmdLineParser.Option ttl = parser.addIntegerOption("ttl");
		CmdLineParser.Option prevcol = parser.addIntegerOption("prevcol");
		CmdLineParser.Option cvmp = parser.addDoubleOption("cvmp");
		CmdLineParser.Option ncvmp = parser.addDoubleOption("ncvmp");
		
		try {
			parser.parse(args);
		} catch (CmdLineParser.OptionException e) {
			System.err.println(e.getMessage());
			errorFound();
		}
		
		Integer generationsCount = (Integer)parser.getOptionValue(generations);
		if (generationsCount != null) {
			Settings.setGenerationsCount(generationsCount);
		}
		Integer individualsInGeneration = (Integer)parser.getOptionValue(individuals);
		if (individualsInGeneration != null) {
			Settings.setIndividualsInGeneration(individualsInGeneration);
		}
		Integer eliteCount = (Integer)parser.getOptionValue(elite);
		if (eliteCount != null) {
			Settings.setEliteCount(eliteCount);
		}
		Integer newGenerationIndividuals = (Integer)parser.getOptionValue(ngi);
		if (newGenerationIndividuals != null) {
			Settings.setNewGenerationIndividuals(newGenerationIndividuals);
		}
		Integer timeToLive = (Integer)parser.getOptionValue(ttl);
		if (timeToLive != null) {
			Settings.setTimeToLive(timeToLive);
		}
		Integer maxPreviousColorsSize = (Integer)parser.getOptionValue(prevcol);
		if (maxPreviousColorsSize != null) {
			Settings.setMaxPreviousColorsSize(maxPreviousColorsSize);
		}
		Double conflictedVerticesMutationProbability = (Double)parser.getOptionValue(cvmp);
		if (conflictedVerticesMutationProbability != null) {
			Settings.setConflictedVerticesMutationProbability(conflictedVerticesMutationProbability);
		}
		Double nonConflictedVerticesMutationProbability = (Double)parser.getOptionValue(ncvmp);
		if (nonConflictedVerticesMutationProbability != null) {
			Settings.setNonConflictedVerticesMutationProbability(nonConflictedVerticesMutationProbability);
		}
		
		if (parser.getOptionValue(gui) != null) {
			Settings.setGui(true);
			new GraphWindow();
		} else {
			String filename = (String)parser.getOptionValue(file);
			if (filename == null) {
				errorFound();
			}
			try {
				Graph graph = new Graph(filename);
				NewHEALAlgorithm algorithm = new NewHEALAlgorithm(new GraphItem(filename, "", 0, 0));
				graph = algorithm.runPerformed(graph);
				
				BufferedWriter writer = new BufferedWriter(new FileWriter("logs/log.txt", true));
				writer.write(filename + " - " + graph.getColors(null).size() + "\n");
				writer.write("id: color\n");
				for (Vertex vertex : graph.getVertices()) {
					writer.write(vertex.getId() + ": " + vertex.getColor() + "\n");
				}
				writer.write("\n\n");
				writer.close();
				
				System.out.println("done stats saved in " + Settings.getFilename());
			} catch (IOException e) {
				System.out.println(e.getMessage());
			}
		}
	}
}
